package com.zeros.mysplashtwo.base;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import com.zeros.mysplashtwo.utils.ToastUtilTwo;
import com.zhy.autolayout.AutoLayoutActivity;

import butterknife.ButterKnife;

/**
 * 适配地址：https://github.com/hongyangAndroid/AndroidAutoLayout
 * on 2017/12/21.
 */

public class BaseActivity extends AutoLayoutActivity {

    private AlertDialog dialog;
    private ProgressDialog mProgressDialog = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        写在此处会报错（Null）因为此句在setContent之前就执行了
//        ButterKnife.bind(this);
    }

    protected void iniBase() {
        getSupportActionBar().setHomeButtonEnabled(true);        //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);  //创建返回键，并实现打开关/闭监听
        getSupportActionBar().setDisplayShowTitleEnabled(false);//隐藏系统默认的Title
    }

    private void backhome(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    /**
     * 1. 显示ProgressDialog
     */
    public void showProgressDialog(String message) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
        }
        mProgressDialog.setMessage(message);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.show();
    }

    /**
     * 2. 隐藏ProgressDialog
     */
    public void hideProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.hide();
        }
    }

    /**
     * 3. 显示AlertDialog
     */
    public void showAlertDialog(String title, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title)
                .setMessage(message)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    /**
     * 4. 吐司
     */
    public void showToast(String message) {
        ToastUtilTwo.showShort(this, message);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mProgressDialog != null)
            mProgressDialog.dismiss();
    }

    /*************--------接口-----***************/
    public interface Convert {
        void transaction(View dialogView, AlertDialog alertDialog);
    }

    public interface DialogOnClickListener {
        void positiveListener(DialogInterface dialog, int which);

        void negativeListener(DialogInterface dialog, int which);
    }

}
